package com.example.woke;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.jakewharton.rxbinding2.view.RxView;
import com.woke.adapter.UnionPayChannelAdapter;
import com.woke.addressactivity.Userinfo_payActivity;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ChannelsActivity extends AppCompatActivity {

    private MyApp application;
    private RecyclerView mChannelRecyView;
    private UnionPayChannelAdapter mChannelAdapter;
    private ArrayList<UnionPayChannel> mUnionPayChannels = new ArrayList<>();
    private TextView mMoneyTv;
    private Button mSureBtn;

    private String payWay, money = "", userId;
    private int mKind = -1;
    private TextView mTitleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channels);
        initView();
        Intent intent = getIntent();
        application = (MyApp) getApplication();
        payWay = intent.getStringExtra("payWay");
        if ("wechat".equals(payWay)) {
            mKind = 1;
            mTitleTv.setText(getString(R.string.title_wechat_pay));
        } else if ("alibaba".equals(payWay)) {
            mKind = 2;
            mTitleTv.setText(getString(R.string.title_alibaba_pay));
        }
        money = intent.getStringExtra("money");
        userId = intent.getStringExtra("userId");

        String money_f = getString(R.string.user_money_f3);
        mMoneyTv.setText(String.format(money_f, money));

        getPayChannel();
    }


    private void initView() {
        findViewById(R.id.channel_image_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTitleTv = (TextView) findViewById(R.id.channel_title);
        mMoneyTv = (TextView) findViewById(R.id.channel_moeny);

        mChannelRecyView = (RecyclerView) findViewById(R.id.channel_recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        mChannelRecyView.setLayoutManager(layoutManager);
        mChannelRecyView.setHasFixedSize(true);
        mChannelRecyView.setNestedScrollingEnabled(false);
        mChannelAdapter = new UnionPayChannelAdapter(ChannelsActivity.this, mUnionPayChannels);
        mChannelAdapter.setChannelType(mKind);
        mChannelRecyView.setAdapter(mChannelAdapter);

        mSureBtn = (Button) findViewById(R.id.channel_sure_pay);
        RxView.clicks(mSureBtn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        createQrCode();
                    }
                });
    }

    private void getPayChannel() {
        HttpMethods.getInstance()
                .getPayChanel(mChannelObserver, Integer.parseInt(userId), 2, mKind);
    }

    private Observer<List<UnionPayChannel>> mChannelObserver = new Observer<List<UnionPayChannel>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(List<UnionPayChannel> value) {
            if (value != null && value.size() > 0) {
                mUnionPayChannels.clear();
                mUnionPayChannels.addAll(value);
                if (mChannelAdapter != null) {
                    mChannelAdapter.setChannelType(mKind);
                    mChannelAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onError(Throwable e) {
            if (e != null) {
                Toast.makeText(ChannelsActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onComplete() {

        }
    };

    private void createQrCode() {
        Intent intent = new Intent(this, Userinfo_payActivity.class);
        intent.putExtra("payWay", String.valueOf(mKind));
        intent.putExtra("userId", userId);
        intent.putExtra("money", money);
        if (mChannelAdapter != null && mChannelAdapter.getWay() != -1) {
            intent.putExtra("channelWay", mChannelAdapter.getWay());
        } else {
            intent.putExtra("channelWay", 1);
        }
        startActivity(intent);
    }
}
